﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnLineExam.BLL;
using System.IO;
using System.Data.OleDb;

namespace OnLineExam.Web.mag.old
{
    public partial class useranswerlist : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string userId = User.Identity.Name;
            var loginuser = new UserManager().GetModel(userId);
            string userName = loginuser.UserName;

            var bll = new OnLineExam.BLL.PaperManager();

            this.DropDownList1.DataTextField = "PaperName";
            this.DropDownList1.DataValueField = "PaperID";
            this.DropDownList1.DataSource = bll.GetList("depid=" + loginuser.DepID);
            this.DropDownList1.DataBind();

            var ds = HHG.DBUtility.DbHelperSQL.Query("select * from view_7");
            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            //生成将要存放结果的Excel文件的名称
            string NewFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
            //转换为物理路径
            NewFileName = Server.MapPath(NewFileName);
            //根据模板正式生成该Excel文件
            File.Copy(Server.MapPath("Module01.xls"), NewFileName, true);
            //建立指向该Excel文件的数据库连接
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + NewFileName + ";Extended Properties='Excel 8.0;'";
            OleDbConnection Conn = new OleDbConnection(strConn);
            //打开连接，为操作该文件做准备
            Conn.Open();
            OleDbCommand Cmd = new OleDbCommand("", Conn);

            //if (f)
            //{
            foreach (GridViewRow DR in GridView1.Rows)
            {
                int a = DR.Cells.Count;


                string XSqlString = "insert into [Sheet1$]";
                XSqlString += "([用户编号],[用户姓名],[试卷编号],[成绩],[考试时间]) values(";
                XSqlString += "'" + (DR.Cells[0].FindControl("lblUserID") as Label).Text + "',";
                XSqlString += "'" + (DR.Cells[1].FindControl("lblUserName") as Label).Text + "',";
                XSqlString += "'" + (DR.Cells[2].FindControl("Label11") as Label).Text + "',";
                XSqlString += "'" + (DR.Cells[3].FindControl("Label4") as Label).Text + "',";
                XSqlString += "'" + (DR.Cells[4].FindControl("Label5") as Label).Text + "')";
                Cmd.CommandText = XSqlString;
                Cmd.ExecuteNonQuery();
            }
            //}


            //操作结束，关闭连接
            Conn.Close();
            //打开要下载的文件，并把该文件存放在FileStream中
            System.IO.FileStream Reader = System.IO.File.OpenRead(NewFileName);
            //文件传送的剩余字节数：初始值为文件的总大小
            long Length = Reader.Length;

            Response.Buffer = false;
            Response.AddHeader("Connection", "Keep-Alive");
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode("答题明细.xls"));
            Response.AddHeader("Content-Length", Length.ToString());

            byte[] Buffer = new Byte[10000];		//存放欲发送数据的缓冲区
            int ByteToRead;											//每次实际读取的字节数

            while (Length > 0)
            {
                //剩余字节数不为零，继续传送
                if (Response.IsClientConnected)
                {
                    //客户端浏览器还打开着，继续传送
                    ByteToRead = Reader.Read(Buffer, 0, 10000);					//往缓冲区读入数据
                    Response.OutputStream.Write(Buffer, 0, ByteToRead);	//把缓冲区的数据写入客户端浏览器
                    Response.Flush();																		//立即写入客户端
                    Length -= ByteToRead;																//剩余字节数减少
                }
                else
                {
                    //客户端浏览器已经断开，阻止继续循环
                    Length = -1;
                }
            }

            //关闭该文件
            Reader.Close();
            //删除该Excel文件
            File.Delete(NewFileName);
        }
    }
}